package com.ruoyi.utils.txy;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.SSL.domain.SslApply;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUtils;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ssl.v20191205.SslClient;
import com.tencentcloudapi.ssl.v20191205.models.*;

import java.util.List;

public class TxySSLUtils {

    public static SslClient createClient(String secretId, String secretKey) {
        // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
        Credential cred = new Credential(secretId, secretKey);
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("ssl.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        return new SslClient(cred, "", clientProfile);
    }

    public static AjaxResult listDescribeCertificates(String secretId, String secretKey, String searchKey) {
        try {
            // 实例化要请求产品的client对象,clientProfile是可选的
            SslClient client = createClient(secretId, secretKey);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DescribeCertificatesRequest req = new DescribeCertificatesRequest();
            if(StrUtil.isNotBlank(searchKey)){
                req.setSearchKey(searchKey);
            }
            // 返回的resp是一个DescribeCertificatesResponse的实例，与请求对象对应
            DescribeCertificatesResponse resp = client.DescribeCertificates(req);
            return AjaxResult.success("查询腾讯云SSL证书成功！", resp.getCertificates());
        } catch (Exception e) {
            return AjaxResult.error("查询腾讯云SSL证书错误："+e.getMessage());
        }
    }

    public static AjaxResult upLoadDescribeCertificates(String secretId, String secretKey, SslApply sslApply, String ptName){
        try {
            // 实例化要请求产品的client对象,clientProfile是可选的
            SslClient client = createClient(secretId, secretKey);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            UploadCertificateRequest req = new UploadCertificateRequest();
            String[] fileArray = sslApply.getSslFilePath().split(",");
            req.setCertificatePrivateKey(FileUtils.readFileContent(fileArray[0]));
            req.setCertificatePublicKey(FileUtils.readFileContent(fileArray[1]));
            req.setAlias(sslApply.getCertificateDomain()+"_"+ptName+"_"+ RandomUtil.randomStringUpper(9));
            // 返回的resp是一个UploadCertificateResponse的实例，与请求对象对应
            UploadCertificateResponse resp = client.UploadCertificate(req);
            Console.log(JSONUtil.toJsonStr(resp));
            return AjaxResult.success("上传腾讯云SSL证书成功！", resp.getCertificateId());
        } catch (Exception e) {
            return AjaxResult.error("上传腾讯云SSL证书域名错误："+ e.getMessage());
        }
    }
}
